#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   MonsterHandbook
@Time    :   2023/03/29 21:20:44
@author  :   Eval
@Version :   1.0
@Describe:   怪物手册
'''

from pygame.locals import *
from lib.scripts.GC import *
from lib.scripts.ui.ShowDiaLog import *
from lib.scripts.Enemys import *
from lib.scripts.Hero import Hero
import pygame,sys

class MonsterHandbook:
    def __init__(self,parent) -> None:
        import main
        self.parent:main.Run = parent
        self.packBG = pygame.Surface((450,450))
        self.packBG.set_alpha(170)
        self.__isActive = False # 手册是否激活, 禁止从外部直接修改
        # 渲染手册时,将怪物的key进行排序,让damage值最低的靠前显示
        self.monsterLv:list[Enemys] = []

        
    def render(self):
        """渲染手册"""
        if not self.isActive():
            return
        self.render_view()
        # 监听事件
        self.events()


    def render_view(self):
        # 手册页面主体背景
        self.parent.win.blit(self.packBG,(200,0))
        # 手册边框
        pygame.draw.rect(self.parent.win,(200,200,0),(200,0,450,450),width=2)
        index = 0
        for monster in self.monsterLv:
            if monster.active:
                pygame.draw.rect(self.parent.win,(50,200,100),(205,index*50+5,440,55),width=1)  # 每行数据的边框
                self.parent.GameFont.render_lineText(240,index*50+10,monster.name,color=(255,210,0),fontSize=14)  # 怪物名称
                self.parent.GameFont.render_multilineText(310,index*50+10,monster.status(),382,fontSize=13,color=(247,245,228),indent=12)  # 道具描述
                self.parent.GameFont.render_lineText(550,index*50+35,f"-{monster.damage}",color=(250,10,10),fontSize=14)  # 对玩家的伤害
                monster.imageUI.update(self.parent,205,index*50+10)  # 渲染怪物精灵
                index += 1

    def events(self):
        pos = pygame.mouse.get_pos()
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif e.type == pygame.KEYDOWN:
                if e.key == K_x or e.key == K_ESCAPE:
                    self.ShowUI()

    def isActive(self):
        """返回手册是否激活状态"""
        return self.__isActive
    
    def clearMonsterBook(self):
        """清空怪物手册缓存"""
        pass

    def ShowUI(self,active=None):
        """关闭/开启怪物手册"""
        if active is not None:
            self.__isActive = not active
        if not len(self.monsterLv):
            hero:Hero = self.parent.hero
            for key in GC.GC_FloorMonsterData:
                monster:Enemys = GC.GC_FloorMonsterData[key]
                monster.damage =  max(monster.atk - hero.defe,1)
                self.monsterLv.append(monster)
            hero:Hero = self.parent.hero
            for x in range(len(self.monsterLv)):
                for y in range(len(self.monsterLv)-x-1):
                    if self.monsterLv[y].damage > self.monsterLv[y+1].damage:
                        self.monsterLv[y],self.monsterLv[y+1] = self.monsterLv[y+1],self.monsterLv[y]
        else:
            # 每次打开都要重新计算伤害
            hero:Hero = self.parent.hero
            for monster in self.monsterLv:
                monster.damage =  max(monster.atk - hero.defe,1)
        self.__isActive = not self.__isActive